package com.cqs.leetcode.other;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class LCP19 {

    public int minimumOperations(String leaves) {
        int len = leaves.length();
        int[][] dp = new int[len][3];
        dp[0][0] = isYellow(leaves.charAt(0));
        dp[0][1] = dp[0][0] + 1 - isYellow(leaves.charAt(1));
        dp[0][2] = dp[0][1] + isYellow(leaves.charAt(2));
        for (int i = 1; i < len; ++i) {
            char c = leaves.charAt(i);
            dp[i][0] = dp[i - 1][0] + isYellow(c);
            dp[i][1] = Math.min(dp[i - 1][0], dp[i - 1][1]) + (1 - isYellow(c));
            dp[i][2] = Math.min(dp[i - 1][1], dp[i - 1][2]) + isYellow(c);
        }
        return dp[len - 1][2];

    }


    /**
     * 'y' 返回1
     * 'w' 返回0
     *
     * @param c
     * @return
     */
    private int isYellow(char c) {
        return c == 'y' ? 1 : 0;
    }

    public int numJewelsInStones(String J, String S) {
        int cnt = 0;
        Set<Character> set = new HashSet<>();
        for (char c : J.toCharArray()) {
            set.add(c);
        }
        for(int i = 0; i < S.length(); ++i){
            if(set.contains(S.charAt(i))){
                ++cnt;
            }
        }
        return cnt;
    }

}
